class User < ActiveRecord::Base

  has_many :user_levels, -> {order 'id desc'}
  has_many :levels, through: :user_levels
  has_many :script_levels, through: :levels
  has_one :teacher_prize
  has_one :teacher_bonus_prize
  
  TYPE_STUDENT = 'student'
  TYPE_TEACHER = 'teacher'
  
  def short_name
    return username if name.blank?
    name.split.first # 'first name'
  end
  
  def teacher?
    self.user_type == TYPE_TEACHER
  end
  
  # can this user edit their own account?
  def can_edit_account?
    return true if teacher? || encrypted_password.present? || oauth?

    # sections_as_student should be a method but I already did that in another branch so I'm avoiding conflicts for now
    sections_as_student = followeds.collect(&:section)
    return true if sections_as_student.empty?

    # if you log in only through picture passwords you can't edit your account
    return !(sections_as_student.all? {|section| section.login_type == Section::LOGIN_TYPE_PICTURE})
  end
  
  
end